﻿using BizModels;
using DataModel;
using IRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Repositories
{
    public class RecruitStateRepository : RepositoryBase, IRecruitStateRepository
    {
        public RecruitStateRepository()
        {
            Context = new HinokyEntities();
        }

        public int AddTodayRecruitState(RecruitStateT recruitState)
        {
            var data = new RecruitCountByDay
            {
                BasicDate = recruitState.BasicDate,
                AgentType = recruitState.AgentType,
                JobType = recruitState.JobType,
                RecruitCount = recruitState.RecruitCount,
                LinkUrl = recruitState.LinkUrl,
                IsDisplay = recruitState.IsDisplay,
                InsertDate = DateTime.Now,
                Description = recruitState.Description
            };

            Context.RecruitCountByDay.Add(data);
            Context.SaveChanges();

            return 1;
        }

        public List<RecruitStateT> GetRecruitState(string startDate, string endDate, string agentType, string jobType)
        {
            var query = from s in Context.RecruitCountByDay
                        where string.Compare(s.BasicDate, startDate, StringComparison.Ordinal) >= 0 && string.Compare(s.BasicDate, endDate, StringComparison.Ordinal) <= 0
                        orderby s.BasicDate ascending
                        select new RecruitStateT
                        {
                            BasicDate = s.BasicDate,
                            AgentType = s.AgentType,
                            JobType = s.JobType,
                            RecruitCount = s.RecruitCount,
                            LinkUrl = s.LinkUrl,
                            IsDisplay = s.IsDisplay,
                            InsertDate = s.InsertDate,
                            Description = s.Description
                        };

            if (!string.IsNullOrEmpty(agentType))
            {
                query = query.Where(s => s.AgentType == agentType);
            }

            if (!string.IsNullOrEmpty(jobType))
            {
                query = query.Where(s => s.JobType == jobType);
            }

            return query.ToList();
        }
    }
}
